<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/8/12
 * Time: 14:03
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Signs extends Base
{
    /**
     * 标签列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('sign')->count();
            $lists = Db::name('sign')
                ->page($Nowpage,$limits)
                ->order('sign_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('');
    }
    
    /**
     * 添加标签
     * @return mixed|\think\response\Json
     */
    public function signAdd(){
        if(Request::instance()->isPost()){
            $sign_name = trim(input('sign_name'), ' ');
            $insert = [
                'sign_name' => $sign_name,
                'sign_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_sign')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('signs/signadd');
    }
    
    /**
     * 编辑标签
     * @return mixed|\think\response\Json
     */
    public function signEdit(){
        $sign_id = input('sign_id');
        if(Request::instance()->isPost()){
            $sign_name = trim(input('sign_name'), ' ');
            $update = [
                'sign_name' => $sign_name,
                'sign_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_sign')->where('sign_id', $sign_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $sign = Db::table('up_sign')
            ->where('sign_id', $sign_id)
            ->find();
        $this->assign([
            'sign' => $sign,
        ]);
        return $this->fetch('signs/signedit');
    }
    
    /**
     * 删除标签
     * @return \think\response\Json
     */
    public function signDel(){
        $sign_id = input('id');
        $res = Db::table('up_sign')->delete($sign_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}